package com.exchange.controller.app.playMent;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.aliyun.oss.OSSClient;
import com.exchange.common.model.User;
import com.exchange.common.model.UserKyc;
import com.exchange.common.model.UserPayment;
import com.exchange.constant.AppConst;
import com.exchange.constant.BizConst;
import com.exchange.interceptor.EnumError;
import com.exchange.util.ResponseData;
import com.exchange.util.aliOSSFileUtil;
import com.jfinal.kit.PropKit;
import com.jfinal.kit.Ret;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.upload.UploadFile;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;

public class PlayMentService {

	/**
	 * 规定:FirstName前者,LastName后者.无关姓名,只按顺序来
	 * payType为微信和支付宝时file必须有,其他payType不能有file
     * file, payType, payName,  account, userName, bankName
     * bankPay,wePay,aliPay,payPal
	 */
	public Ret add(User user, UploadFile f, String payType, String payName, String account, String userName, String bankName) {
		// 添加支付方式必须通过kyc c1认证,且支付的姓名必须与实名认证的一致
		int v = user.getVerified();
		if (v < 3) {
			return Ret.fail("msg", EnumError.C1_V_NO_CONDUCT.getCode());
		}
		UserKyc userKyc = UserKyc.dao.findFirst("SELECT firstName,lastName FROM t_user_kyc WHERE userId = ?",user.getId());
		String oldName = StrUtil.concat(Boolean.FALSE, userKyc.getFirstName(), userKyc.getLastName());
		if (!StrUtil.equals(userName, oldName)) {
			return Ret.fail("msg", EnumError.PAYMENT_IS_DIFFERENT_WITH_KYC.getCode());
		}
		
		String picUrl = null;
		if(StrUtil.equals(BizConst.PayTapy.WEPAY, payType) || StrUtil.equals(BizConst.PayTapy.ALIPAY, payType)) {
			if(BeanUtil.isEmpty(f.getFile())) {
				return Ret.fail("msg", EnumError.PAYMENT_QRCODE_NULL.getCode());
			}
			OSSClient ossClient = aliOSSFileUtil.getOSSClient();
			picUrl = aliOSSFileUtil.uploadImg(ossClient, f.getFile(), PropKit.get("bucketApp"));
		}else {
			if(StrUtil.isBlank(bankName)) {
				return Ret.fail("msg", EnumError.PAYMENT_QRCODE_NULL.getCode());
			}
		}
		
		new UserPayment().setUserId(user.getId()).setPayType(payType).setPayName(payName)
				.setAddress(picUrl).setAccount(account).setUserName(userName)
				.setBankName(bankName).setCreateTime(new Date()).save();

		return Ret.ok();
	}

	public Ret delete(User user, Integer id) {
		Db.delete("DELETE FROM t_user_payment WHERE id = ? AND userId = ?", id, user.getId());
		return Ret.ok();
	}

	public Ret priority(User user, Integer id){
		//查询要修改的
		UserPayment userPayment = UserPayment.dao.findFirst("SELECT * FROM t_user_payment WHERE id = ? AND userId = ? AND state != 0 ", id, user.getId());
		if(BeanUtil.isEmpty(userPayment)) {
			return Ret.fail("msg", EnumError.PAYMENT_NULL.getCode());
		}
		//将当前用户所有支付方式更新为非默认
		Db.update("UPDATE t_user_payment SET state = 1 WHERE id = ? AND userId = ? AND state = ?", id, user.getId(), 2);
		//设置默认支付方式
		userPayment.setState(3).update();
		
        return Ret.ok();
    }

	public Ret list(User user){
		List<UserPayment> userPaymentList = UserPayment.dao.find("SELECT * FROM t_user_payment WHERE userId = ? ", user.getId());
		return Ret.ok("paymentList",userPaymentList);
    }

	public Ret type(User user){        
		List<String> payTypeList = Db.query("SELECT payType FROM t_user_payment WHERE userId = ? AND state != 0 GROUP BY payType", user.getId());
		return Ret.ok("payTypeList",payTypeList);
    }





}
